home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
DDJMAG
/
DDJ9310.ZIP
/
1993-OCT.ZIP
/
CPPMAN.ASC
< prev
next >
Wrap
Text File
|
1993-09-17
|
4KB
|
221 lines
_C++MANIPULATORS AND APPLICATORS_
by Reginald B. Charney
Example 1:
(a)
// quote argument string and then
// output it.
ostream& qStr(char* s)
{ return cout<<"'"<<s<<"'"; }
cout << "Output is a " <<
qStr("string") << ".";
(b)
Output is a 'string'.
(c)
'string'Output is a 0x48f2.
Example 2:
(a)
typedef ostream OS; // an abbrev
// quote argument string and then
// output it.
OS& aMF(OS& os,char* s)
{ return os << "'" << s << "'"; }
aMF(cout, "string");
(b)
outputs on
cout the value:
'string'
Example 3:
typedef long (*FP)(int, int);
// af is applicator function
long af(FP f, int i, int j)
{ return (*f)(i,j); }
long sum(int i, int j)
{ return i+j; }
long dif(int i, int j)
{ return i-j; }
af(sum,2,3); // returns (long)5
af(dif,9,5); // returns (long)4
Example 4:
typedef ostream OS; // an abbrev
template<class T>
class OMC // output manip class
{
typedef OS& (*MF)(OS&, T);
MF mf; // manip function
T a; // arg of type T
public:
OMC(MF mmf,T aa)
: mf(mmf), a(aa) { }
friend OS& operator <<
(OS& os, const OMC<T>& mc)
{ return (*mc.mf)(os,mc.a); }
};
Example 5:
(a)
// define manipulator interface
// for manipulator function aMF.
OMC<char*> aMI(char* s)
{ return OMC<char*>(aMF,s); }
cout << "Value "<< aMI("string");
(b)
Value 'string'
Example 6:
#include <iostream.h>
typedef ostream OS; // an abbrev
// qSTR - manip function
OS& qSTR(OS& os,char* s)
{ return os << "'" << s << "'"; }
// OMC - Output Manipulator Class
template<class T> class OMC {
typedef OS& (*MF)(OS&, T);
MF mf; // manipulator fcn
T a; // arg of type T
public:
OMC(MF mmf,T aa)
: mf(mmf), a(aa) { }
friend OS& operator <<
(OS& os, const OMC<T>& mc)
{ return (*mc.mf)(os,omc.a); }
};
// qStr - manip interface
// for manip function qSTR
OMC<char*> qStr(char* s)
{ return OMC<char*>(qSTR,s); }
// sample output expression
cout << "Output is a " <<
qStr("string") << "\n";
Example 7:
#include <iostream.h>
#include <iomanip.h>
typedef ostream OS; // an abbrev
typedef char* CP; // single token
// qSTR - manip function
OS& qSTR(OS& os,CP s)
{ return os << "'" << s << "'"; }
// qStr - manip interface
// for manip function qSTR
OMANIP<CP> qStr(CP s)
{ return OMANIP<CP>(qSTR,s); }
// sample output expression
cout << "Output is a "
<< qStr("string") << "\n";
Example 8:
#include <iostream.h>
#include <iomanip.h>
typedef ostream OS; // an abbrev
typedef char* CP; // single token
IOMANIPdeclare(CP);
// qSTR - manip function
OS& qSTR(OS& os,CP s)
{ return os << "'" << s << "'"; }
// qStr - manip interface
// for manipulator fcn qSTR
OMANIP(CP) qStr(CP s)
{ return OMANIP(CP)(qSTR,s); }
// sample output expression
cout << "Output is a "
<< qStr("string") << "\n";
Example 9:
typedef ostream OS; // an abbrev
template<class T> class OAC {
typedef OS& (*MF)(OS&, T);
MF mf;
public:
OAC(MF mmf) : mf(mmf) { }
OMC<T> operator()(T a)
{ return OMC<T>(mf,a); }
};
Example 10:
#include <iostream.h>
#include <iomanip.h>
typedef ostream OS; // an abbrev
typedef char* CP; // single token
// qSTR - manip function
OS& qSTR(OS& os,CP s)
{ return os << "'" << s << "'"; }
OAPP<CP> qStr = qSTR;
// sample output expression
cout << "Output is a "
<< qStr("string") << "\n";